package com.shiwaixiangcun.authz.config;

import com.shiwaixiangcun.authz.RabbitComponentNames;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2017/9/1.
 */
@Configuration
public class RabbitConfig {

    @Value("${rabbitmq.address}")
    private String rabbitmqAddress;
    @Value("${rabbitmq.username}")
    private String rabbitmqUsername;
    @Value("${rabbitmq.password}")
    private String rabbitmqPassword;
    @Value("${rabbitmq.virtual-host}")
    private String virtualHost;
    @Value("${rabbitmq.vport}")
    private Integer rabbitmqVport;

    @Bean
    public ConnectionFactory rabbitConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitmqAddress);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setUsername(rabbitmqUsername);
        connectionFactory.setPassword(rabbitmqPassword);
        connectionFactory.setPort(rabbitmqVport);
        return connectionFactory;
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(rabbitConnectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(rabbitConnectionFactory());
    }

    @Bean
    public FanoutExchange exchange() {
        return new FanoutExchange(rabbitComponentNames().getCustomerAppLoginSuccessExchange(), true, false);
    }

    /**
     * durable:是否持久化
     * exclusive: 仅创建者可以使用的私有队列，断开后自动删除
     * auto_delete: 当所有消费客户端连接断开后，是否自动删除队列
     *
     * @return
     */
    @Bean
    public Queue loginSuccessPropertyQueue() {
        return new Queue(rabbitComponentNames().getLoginSuccessPropertyQueue(), true, false, false);
    }

    @Bean
    public Queue loginSuccessHealthQueue() {
        return new Queue(rabbitComponentNames().getLoginSuccessHealthQueue(), true, false, false);
    }

    @Bean
    public Queue loginSuccessMarketQueue() {
        return new Queue(rabbitComponentNames().getLoginSuccessMarketQueue(), true, false, false);
    }

    @Bean
    public Binding bindingPropertyQueue() {
        return BindingBuilder.bind(loginSuccessPropertyQueue()).to(exchange());
    }

    @Bean
    public Binding bindingHealthQueue() {
        return BindingBuilder.bind(loginSuccessHealthQueue()).to(exchange());
    }

    @Bean
    public Binding bindingMarketQueue() {
        return BindingBuilder.bind(loginSuccessMarketQueue()).to(exchange());
    }

    @Bean
    public RabbitComponentNames rabbitComponentNames() {
        return new RabbitComponentNames();
    }

}
